package com.briup.ch02;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.junit.Before;
import org.junit.Test;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @author yuxi
 * @version 1.0
 * @date 2025/11/6
 * @Description:
 */
public class DriudTest {
    Properties properties = new Properties();
    @Before
    public void setUp(){
        InputStream is = DriudTest.class.getClassLoader().getResourceAsStream("druid.properties");
        try {
            properties.load(is);
            //测试看一下
            properties.forEach((k,v)-> System.out.println(k+"="+v));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Test
    public void testDruid() {
        Connection conn = null;
        try {
            DataSource ds = DruidDataSourceFactory.createDataSource(properties);
            System.out.println(ds);
            conn = ds.getConnection();
            System.out.println(conn);
            System.out.println(ds);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
